from django import forms
from django.contrib import auth

class LoginForm(forms.Form):
    username = forms.CharField()
    password = forms.CharField(widget=forms.PasswordInput)

    def clean(self):
        if auth.authenticate(username=self.cleaned_data["username"], 
                password=self.cleaned_data["password"]) is None:
            raise forms.ValidationError("That username or password is not \
            correct")
        return self.cleaned_data

class GradeForm(forms.Form):
    grade = forms.IntegerField()
    comments = forms.CharField(widget=forms.Textarea)

    def clean_grade(self):
        grade = self.cleaned_data["grade"]
        if grade < 0 or grade > 100:
            raise forms.ValidationError("Grade must be between 0 and 100")
        return grade


class SubmitForm(forms.Form):
    file = forms.FileField()
    grade=forms.IntegerField(label="Give Yourself a Grade")

    def clean_grade(self):
        grade = self.cleaned_data["grade"]
        if grade < 0 or grade > 100:
            raise forms.ValidationError("Grade must be between 0 and 100")
        return grade

class NewUserForm(forms.Form):
    username = forms.CharField()
    password = forms.CharField(widget=forms.PasswordInput)
    password2 = forms.CharField(widget=forms.PasswordInput,
        label="Re-type password")
    TYPES = (
            ("Student", "Student"),
            ("TA", "TA"),
            ("Professor", "Professor")
            )
    type = forms.ChoiceField(choices=TYPES)

    def clean_username(self):
        if len(auth.models.User.objects.filter(
            username=self.cleaned_data["username"]))!= 0:
            raise forms.ValidationError("That username is taken")
        return self.cleaned_data["username"]

    def clean(self):
        if self.cleaned_data.get(
                "password", "") != self.cleaned_data.get("password2", ""):
            raise forms.ValidationError("Passwords don't match")
        return self.cleaned_data

class NewAssignmentForm(forms.Form):
    name = forms.CharField()
    description = forms.CharField(widget=forms.Textarea)
